﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using P113_GtMotion;
using System.Management;
using Prism.Commands;
using Prism.Services.Dialogs;
using System.IO;
using System.Collections.ObjectModel;
namespace GTSDemo.ViewModels
{
    public class MainWindowVM : BindableBase
    {
        IDialogService _dialogService;
        private HardwareHelper _hardwareHelper => HardwareHelper.GetHardwareHelper();
        private string _title = "GTSDemo";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public DelegateCommand LoadedCommand { get; }
        public ObservableCollection<CardInfo> Cards { get; set; } = new ObservableCollection<CardInfo>();
        public MainWindowVM(IDialogService service)
        {
            LoadedCommand = new DelegateCommand(Loaded);
            _dialogService = service;
        }
        private void Loaded()
        {
            var driveInfos = DriveInfo.GetDrives();
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");
            int count = 0;
            foreach (ManagementObject service in searcher.Get())
            {
                if (service["Description"] == null) continue;
                System.Diagnostics.Debug.WriteLine(service["Description"].ToString());
                if (service["Description"].ToString().ToLower().Contains("googol"))
                    count++;
            }
            if (count <= 0)
            {
                var message = "当前无法搜寻到有效的GTS运动控制卡.是否使用虚拟模式运行?";
                _dialogService.ShowDialog("MessageDialog", new DialogParameters($"message={message}"), r =>
                {
                    if (r.Result == ButtonResult.OK)
                    {
                        count = 1;
                        _hardwareHelper.CardCount = count;
                        _hardwareHelper.Initialization();
                        Cards.Add(new CardInfo(0, "Card1"));
                    }
                    else
                    {
                        Environment.Exit(0);
                    }
                });
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    Cards.Add(new CardInfo(i, $"Card{i + 1}"));
                }
                _hardwareHelper.CardCount = count;
                _hardwareHelper.Initialization();
            }
            foreach (var card in Cards)
            {
                card.ChekedEventHandler += Card_ChekedEventHandler;
            }
            Cards.First().IsCheked = true;  //默认选择第一张卡
        }

        private void Card_ChekedEventHandler(object? sender, EventArgs e)
        {
            var info = sender as CardInfo;
            if (info == null) return;
            _hardwareHelper.CurrentCardIndex = info.CardIndex;
        }
    }

    public class CardInfo
    {
        public CardInfo(int cardIndex,string name)
        {
            CardIndex = cardIndex;
            Name = name;
        }
        public int CardIndex { get; set; }
        public string Name { get; set; }
        private bool _isChecked;
        public bool IsCheked 
        {
            get => _isChecked;
            set
            {
                if (_isChecked != value && _isChecked == false)
                { 
                    ChekedEventHandler?.Invoke(this, EventArgs.Empty);
                }
                _isChecked = value;
            }
        }
        public event EventHandler ChekedEventHandler;
    }
}
